// 生成模拟用户数据
const generateUsers = (count) => {
  const users = []
  const roles = ['admin', 'user', 'guest']
  const statuses = ['active', 'inactive', 'locked']
  
  for (let i = 1; i <= count; i++) {
    users.push({
      id: i,
      username: `user${i}`,
      nickname: `用户${i}`,
      email: `user${i}@example.com`,
      phone: `1380013${String(i).padStart(4, '0')}`,
      role: roles[Math.floor(Math.random() * roles.length)],
      status: statuses[Math.floor(Math.random() * statuses.length)],
      lastLogin: new Date(Date.now() - Math.floor(Math.random() * 30) * 86400000).toISOString(),
      createTime: new Date(Date.now() - Math.floor(Math.random() * 365) * 86400000).toISOString()
    })
  }
  
  return users
}

// 用户相关的模拟数据
export function setupUserMock(mock) {
  // 模拟的用户数据
  const users = generateUsers(50)
  
  // 模拟登录
  mock.onPost('/api/user/login').reply((config) => {
    const { username, password } = JSON.parse(config.data)
    
    if (username === 'admin' && password === 'admin123') {
      return [
        200,
        {
          code: 200,
          message: '登录成功',
          data: {
            token: 'mock-token-' + Date.now(),
            user: {
              id: 1,
              username: 'admin',
              nickname: '管理员',
              email: 'admin@example.com',
              role: 'admin'
            }
          }
        }
      ]
    } else {
      return [
        200,
        {
          code: 401,
          message: '用户名或密码错误',
          data: null
        }
      ]
    }
  })
  
  // 获取用户列表
  mock.onGet('/api/user/list').reply((config) => {
    const { page = 1, pageSize = 10, keyword = '' } = config.params || {}
    
    let filteredUsers = [...users]
    
    // 关键词过滤
    if (keyword) {
      filteredUsers = filteredUsers.filter(
        user => user.username.includes(keyword) || 
                user.nickname.includes(keyword) || 
                user.email.includes(keyword)
      )
    }
    
    // 分页
    const total = filteredUsers.length
    const start = (page - 1) * pageSize
    const end = start + Number(pageSize)
    const pageData = filteredUsers.slice(start, end)
    
    return [
      200,
      {
        code: 200,
        message: 'success',
        data: {
          list: pageData,
          pagination: {
            page: Number(page),
            pageSize: Number(pageSize),
            total
          }
        }
      }
    ]
  })
  
  // 获取当前用户信息
  mock.onGet('/api/user/info').reply(() => {
    return [
      200,
      {
        code: 200,
        message: 'success',
        data: {
          id: 1,
          username: 'admin',
          nickname: '管理员',
          email: 'admin@example.com',
          phone: '13800138000',
          role: 'admin',
          avatar: '',
          permissions: ['dashboard', 'user', 'equipment', 'command', 'company', 'agreement']
        }
      }
    ]
  })
} 